package com.power.sifa.supervise.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ding.service.Di_todoRemoteService;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.domain.YesNo;
import com.power.flowable.api.IPersonalFeign;
import com.power.flowable.model.ProcessNextExecutor;
import com.power.sifa.approve.model.Apl_main;
import com.power.sifa.approve.model.enumeration.St_apply;
import com.power.sifa.approve.service.Apl_mainService;
import com.power.sifa.supervise.api.IBusinessSupervise;
import com.power.sifa.supervise.model.Apl_supervise;
import com.power.sifa.supervise.service.Apl_superviseService;
import com.power.sifa.supervise.service.BusinessSuperviseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@RestController
public class BusinessSuperviseController implements IBusinessSupervise {

    @Resource
    private BusinessSuperviseService businessSuperviseService;

    @Resource
    private IPersonalFeign personalFeign;

    @Resource
    private Apl_mainService apl_mainService;

    @Resource
    private Apl_superviseService apl_superviseService;

    @Resource
    private Di_todoRemoteService di_todoRemoteService;

    @Override
    public ResponseDTO startBusinessProced(String applyid, String proctype, String userid, Map<String, Object> docfield) {
        try {
            Map<String, String> result = this.businessSuperviseService.startBusinessProced(applyid, proctype, userid, docfield);
            return ResponseDTO.succData(result);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("/businesssupervise/startBusinessProced 启动一个业务子流程失败 ->  msg[{}]", e.getMessage());
            return ResponseDTO.wrap(500, e.getMessage());
        }
    }

    @Override
    public ResponseDTO completeToNext(String applyid, String taskid, String userid) {
        Assert.notNull(applyid, "applyid非空");
        Apl_main apl_main = this.apl_mainService.getById(applyid);
        // 调用保存流程变量
        Apl_supervise apl_supervise = this.apl_superviseService.getById(applyid);
        if (ObjectUtil.isNotNull(apl_main) && ObjectUtil.isNotNull(apl_supervise)) {
            // 流程进行下一步的环节
            Map variables = new HashMap();
            variables.put("firstid", apl_supervise.getFirstid());
            ProcessNextExecutor body = new ProcessNextExecutor();
            body.setApplyid(applyid);
            String handletext;
            handletext = "审查完成";
            body.setRemark("审查完成");
            body.setHandletext("审查完成");
            body.setVariable(variables);
            ResponseDTO processNextDTO = personalFeign.taskExec(handletext, apl_main.getFormid(), taskid, userid, body);
            if (processNextDTO.getCode() == 0) {
                List<String> todoList = new ArrayList<>();
                // 审查阶段的 赣政通待办id
                String reviewtodoid = apl_supervise.getReviewtodoid();
                if (StringUtils.isNotEmpty(reviewtodoid)) {
                    todoList = Arrays.asList(reviewtodoid.split(","));
                }
                if (CollUtil.isNotEmpty(todoList)) {
                    todoList.stream().forEach(todoId -> this.di_todoRemoteService.afterValidateFinishTodo(todoId));
                    log.info("关闭代办任务完成");
                }
                return ResponseDTO.succMsg("完成操作成功");
            }  else {
                return ResponseDTO.wrap(500, "完成操作错误，当前任务无法进入下一步");
            }
        }
        return  ResponseDTO.wrap(500, "完成操作错误");
    }

    @Override
    public ResponseDTO completeMaterial(String applyid, String taskid, String userid) {
        Assert.notNull(applyid, "applyid非空");
        Apl_main apl_main = this.apl_mainService.getById(applyid);
        // 调用保存流程变量
        Apl_supervise apl_supervise = this.apl_superviseService.getById(applyid);
        if (ObjectUtil.isNotNull(apl_main) && ObjectUtil.isNotNull(apl_supervise)) {
            List<String> todoList = new ArrayList<>();
            // 决定阶段的赣政通代办id
            String rectifitodoid = apl_supervise.getRectifitodoid();
            if (StringUtils.isNotEmpty(rectifitodoid)) {
                todoList = Arrays.asList(rectifitodoid.split(","));
            }
            if (CollUtil.isNotEmpty(todoList)) {
                todoList.stream().forEach(todoId -> this.di_todoRemoteService.afterValidateFinishTodo(todoId));
                log.info("关闭代办任务完成");
            }
            // 设置整改完成字段
            this.apl_superviseService.updateFinishState(apl_supervise.getId(),YesNo.YES);
            return ResponseDTO.succMsg("执行整改完成成功");
        }
        return  ResponseDTO.wrap(500, "执行整改完成失败");
    }

    @Override
    public ResponseDTO completeEnd (String applyid, String taskid, String userid) {
        Assert.notNull(applyid, "applyid非空");
        Apl_main apl_main = this.apl_mainService.getById(applyid);
        // 调用保存流程变量
        Apl_supervise apl_supervise = this.apl_superviseService.getById(applyid);
        if (ObjectUtil.isNotNull(apl_main) && ObjectUtil.isNotNull(apl_supervise)) {
            // 流程进行下一步的环节
            Map variables = new HashMap();
            variables.put("firstid", apl_supervise.getFirstid());
            ProcessNextExecutor body = new ProcessNextExecutor();
            body.setApplyid(applyid);
            String handletext;
            handletext = "提交完成";
            body.setRemark("提交完成");
            body.setHandletext("提交完成");
            body.setVariable(variables);
            ResponseDTO processNextDTO = personalFeign.taskExec(handletext, apl_main.getFormid(), taskid, userid, body);
            if (processNextDTO.getCode() == 0) {
                apl_main.setStatus(St_apply.FINISH);
                this.apl_mainService.saveOrUpdate(apl_main);
                return ResponseDTO.succMsg("完成操作成功");
            }  else {
                return ResponseDTO.wrap(500, "完成操作错误，当前任务无法进入下一步");
            }
        }
        return  ResponseDTO.wrap(500, "完成操作错误");
    }


}
